package com.xhj.sportsnews.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.xhj.sportsnews.R;
import com.xhj.sportsnews.fragment.EventFragment;
import com.xhj.sportsnews.fragment.HeadineFragment;
import com.xhj.sportsnews.fragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    private FragmentManager manager;

    private FragmentTransaction transaction;
    private List<Fragment>fragmentList;
    private NavigationView navigationView;
    private SlidingPaneLayout sliding;
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationView = (NavigationView) findViewById(R.id.main_navigationview);
        navigationView.setItemIconTintList(null);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        fragmentList = new ArrayList<>();
        fragmentList.add(new NewsFragment());
        fragmentList.add(new HeadineFragment());
        fragmentList.add(new EventFragment());
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_container,fragmentList.get(0));
        transaction.add(R.id.fragment_container,fragmentList.get(1));
        transaction.add(R.id.fragment_container,fragmentList.get(2));
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(1));
        transaction.commit();

        //侧滑抽屉不能滑出
//        sliding = ((SlidingPaneLayout) findViewById(R.id.slidingPaneLayout));
        relativeLayout.setOnTouchListener(this);
    }

    public void btnClick(View view){
        transaction = manager.beginTransaction();
        int number = 0;
        switch (view.getId()){
            case R.id.btn_01:
                number = 0;
                break;
            case R.id.btn_02:
                number = 1;
                break;
            case R.id.btn_03:
                number = 2;
                break;
        }
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == number){
                transaction.show(fragmentList.get(i));
            }else {
                transaction.hide(fragmentList.get(i));
            }
        }

        transaction.commit();

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (motionEvent.getX() > 0) {
                    relativeLayout.requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return true;
    }
}
